<template>
  <el-dialog
    :title="!dataForm.newHouseid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地区id" prop="mapid">
      <el-input v-model="dataForm.mapid" placeholder="地区id"></el-input>
    </el-form-item>
    <el-form-item label="新房价格" prop="newHouseprice">
      <el-input v-model="dataForm.newHouseprice" placeholder="新房价格"></el-input>
    </el-form-item>
    <el-form-item label="新房面积" prop="newHousearea">
      <el-input v-model="dataForm.newHousearea" placeholder="新房面积"></el-input>
    </el-form-item>
    <el-form-item label="新房户型(1层、2层、3层、4层)" prop="newHouseroom">
      <el-input v-model="dataForm.newHouseroom" placeholder="新房户型(1层、2层、3层、4层)"></el-input>
    </el-form-item>
    <el-form-item label="新房类型(1.住宅 2.别墅 3.商业 4.写字楼)" prop="newHousetype">
      <el-input v-model="dataForm.newHousetype" placeholder="新房类型(1.住宅 2.别墅 3.商业 4.写字楼)"></el-input>
    </el-form-item>
    <el-form-item label="新房状态(1.未开盘 2.在售 3.售罄)" prop="newHousestatus">
      <el-input v-model="dataForm.newHousestatus" placeholder="新房状态(1.未开盘 2.在售 3.售罄)"></el-input>
    </el-form-item>
    <el-form-item label="新房装修(1.带装修 2.毛坯房)" prop="newHousedeco">
      <el-input v-model="dataForm.newHousedeco" placeholder="新房装修(1.带装修 2.毛坯房)"></el-input>
    </el-form-item>
    <el-form-item label="新房特色(1.近地铁 2.环境好 3.低价)" prop="newHousefeat">
      <el-input v-model="dataForm.newHousefeat" placeholder="新房特色(1.近地铁 2.环境好 3.低价)"></el-input>
    </el-form-item>
    <el-form-item label="新房图片" prop="newHouseimg">
      <el-input v-model="dataForm.newHouseimg" placeholder="新房图片"></el-input>
    </el-form-item>
    <el-form-item label="新房简介" prop="newHousesyno">
      <el-input v-model="dataForm.newHousesyno" placeholder="新房简介"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          newHouseid: 0,
          mapid: '',
          newHouseprice: '',
          newHousearea: '',
          newHouseroom: '',
          newHousetype: '',
          newHousestatus: '',
          newHousedeco: '',
          newHousefeat: '',
          newHouseimg: '',
          newHousesyno: ''
        },
        dataRule: {
          mapid: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          newHouseprice: [
            { required: true, message: '新房价格不能为空', trigger: 'blur' }
          ],
          newHousearea: [
            { required: true, message: '新房面积不能为空', trigger: 'blur' }
          ],
          newHouseroom: [
            { required: true, message: '新房户型(1层、2层、3层、4层)不能为空', trigger: 'blur' }
          ],
          newHousetype: [
            { required: true, message: '新房类型(1.住宅 2.别墅 3.商业 4.写字楼)不能为空', trigger: 'blur' }
          ],
          newHousestatus: [
            { required: true, message: '新房状态(1.未开盘 2.在售 3.售罄)不能为空', trigger: 'blur' }
          ],
          newHousedeco: [
            { required: true, message: '新房装修(1.带装修 2.毛坯房)不能为空', trigger: 'blur' }
          ],
          newHousefeat: [
            { required: true, message: '新房特色(1.近地铁 2.环境好 3.低价)不能为空', trigger: 'blur' }
          ],
          newHouseimg: [
            { required: true, message: '新房图片不能为空', trigger: 'blur' }
          ],
          newHousesyno: [
            { required: true, message: '新房简介不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.newHouseid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.newHouseid) {
            this.$http({
              url: this.$http.adornUrl(`/house/newhouse/info/${this.dataForm.newHouseid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.mapid = data.newhouse.mapid
                this.dataForm.newHouseprice = data.newhouse.newHouseprice
                this.dataForm.newHousearea = data.newhouse.newHousearea
                this.dataForm.newHouseroom = data.newhouse.newHouseroom
                this.dataForm.newHousetype = data.newhouse.newHousetype
                this.dataForm.newHousestatus = data.newhouse.newHousestatus
                this.dataForm.newHousedeco = data.newhouse.newHousedeco
                this.dataForm.newHousefeat = data.newhouse.newHousefeat
                this.dataForm.newHouseimg = data.newhouse.newHouseimg
                this.dataForm.newHousesyno = data.newhouse.newHousesyno
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/newhouse/${!this.dataForm.newHouseid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'newHouseid': this.dataForm.newHouseid || undefined,
                'mapid': this.dataForm.mapid,
                'newHouseprice': this.dataForm.newHouseprice,
                'newHousearea': this.dataForm.newHousearea,
                'newHouseroom': this.dataForm.newHouseroom,
                'newHousetype': this.dataForm.newHousetype,
                'newHousestatus': this.dataForm.newHousestatus,
                'newHousedeco': this.dataForm.newHousedeco,
                'newHousefeat': this.dataForm.newHousefeat,
                'newHouseimg': this.dataForm.newHouseimg,
                'newHousesyno': this.dataForm.newHousesyno
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
